/**
 * CustomTooltips is used the show the tooltips in the seletion screen.
 * Selection criteria is loaded from the model then parsed and displayed
 * using this class 
 * @version     1.0
 * @date		02/15/2011
 * @author   	Mohammed H Al-Darwish
 * @see         Selection_Report
 */
package com.aramco.aviation.common.view.SelectionComponents.Popup.tooltips
{
	import com.aramco.aviation.common.model.Common_ModelLocator;
	import com.aramco.aviation.common.model.FlexConstants;
	import com.aramco.aviation.common.model.vo.selection.SelectionVo;
	import com.aramco.aviation.common.model.vo.selection.SingleMultipleSelectionVo;
	import com.aramco.aviation.common.view.SelectionComponents.Popup.tooltips.HTMLToolTip;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.Button;
	import mx.controls.ToolTip;
	import mx.core.UIComponent;
	import mx.effects.Effect;
	import mx.effects.Fade;
	import mx.managers.ToolTipManager;
	
	public class CustomTooltips
	{
		public function CustomTooltips()
		{
		}
		
		private static var myTip:ToolTip = new ToolTip();
		private static var shown:Boolean = false; 
		public static function getCustomToolTip(key:String):void
		{
			ToolTipManager.enabled = true;// Optional. Default value is true.
			ToolTipManager.showDelay = 2000;// Display immediately.
			ToolTipManager.hideDelay = 1000; // Hide after 3 seconds of being viewed.
			
			var selection:ArrayCollection = Common_ModelLocator.getInstance().selections;
			for(var x:int = 0; x < selection.length; x++)
			{
				if((selection[x] as SelectionVo).key == key)
				{
					var s:String = "These buttons let you save, exit, or continue with the current operation."
					myTip = ToolTipManager.createToolTip(s,10,10) as ToolTip;
					myTip.setStyle("backgroundColor",0xFFCC00);
					myTip.width = 150;
					myTip.height = 200;
					shown=true;
				}
			}
		}
		
		public static function destroyToolTip():void {
			if(myTip != null && shown == true)
				ToolTipManager.destroyToolTip(myTip);
		}
		
		
		private static var singleMutlipleHeader:String = "<font size='+2'><b>Single / Multiple Selections:</b></font><br>";
		private static var singleHeader:String = "<font size='+2'><b>Single Selection:</b></font><br>";
		private static var rangeHeader:String = "<font size='+2'><b>Range Selections:</b></font><br>";
		private static var included:String = "<font color='#287B28'>[Included]</font>";
		private static var excluded:String = "<font color='#CC0033'>[Excluded]</font>";
		private static var empty:String = "<font color='#9BAFB9'>EMPTY</font>";
		private static var tip1:String;
		
		/**
		 * This method will search in the model if there is selection for input key (variableName).
		 * If there is a selection, it will create tooltip and it will assign it to the input button.
		 * @param : String, UIComponent
		 * @return : 
		 * @exception:
		 * @author: Mohammed H Al-Darwish
		 **/
		public static function getToolTip( key:String, button:UIComponent):void
		{
			var _commonModel:Common_ModelLocator = Common_ModelLocator.getInstance();
			if(_commonModel.selections != null && _commonModel.selections.length >0)
			{
				ToolTipManager.enabled = true;// Optional. Default value is true.
				ToolTipManager.showDelay = 10;// Display immediately.
				ToolTipManager.hideDelay = 18000; // Hide after 8 seconds of being viewed.
				ToolTipManager.showEffect =new Fade();
				ToolTipManager.toolTipClass = HTMLToolTip;
				
				var selection:ArrayCollection = _commonModel.selections;
				
				if (selection.length > 0)
				{
					for(var x:int = 0; x < selection.length; x++)
					{
						if((selection[x] as SelectionVo).key == key)
						{
							
							// SINGLE_MULTIPLE_DYN_SELECTION
							if (((selection[x] as SelectionVo).selectionType == FlexConstants.TEXTINPUT_SELEC)
								|| ((selection[x] as SelectionVo).selectionType == FlexConstants.POPUP_CALLMONTH_SINGLE_SELEC))
							{
								//Input Name Header
								tip1 = "<font color='#076baa' size='+4'><b>" + (selection[x] as SelectionVo).inputName+ "</b></font><br>";
								
								//Single Mutitple Header
								tip1 += singleHeader;
								if((selection[x] as SelectionVo).value.singleMultipleSelection.length > 0)
								{
									tip1+= "<ul color='#076baa'>";
									var records:ArrayCollection = (selection[x] as SelectionVo).value.singleMultipleSelection;
									for(var y = 0; y < records.length ; y++)
									{
										if((records[y] as SingleMultipleSelectionVo).included == true)
										{
											tip1 += "<li>" + (records[y] as SingleMultipleSelectionVo).VALUE + "&nbsp;" + included + "</li>";
										}
										else
										{
											tip1 += "<li>" + (records[y] as SingleMultipleSelectionVo).VALUE + "&nbsp;" + excluded + "</li>";
										}	
									}
									tip1+= "</ul>";
									button.toolTip = tip1;
								}
								else
								{
									button.toolTip = null;
								}
							}
							
							else
							{
								//Input Name Header
								tip1 = "<font color='#076baa' size='+4'><b>" + (selection[x] as SelectionVo).inputName+ "</b></font><br>";
								
								//Single Mutitple Header
								tip1 += singleMutlipleHeader;
								if((selection[x] as SelectionVo).value.singleMultipleSelection.length > 0)
								{
									tip1+= "<ul color='#076baa'>";
									var records:ArrayCollection = (selection[x] as SelectionVo).value.singleMultipleSelection;
									for(var y = 0; y < records.length ; y++)
									{
										if((records[y] as SingleMultipleSelectionVo).included == true)
										{
											tip1 += "<li>" + (records[y] as SingleMultipleSelectionVo).VALUE + "&nbsp;" + included + "</li>";
										}
										else
										{
											tip1 += "<li>" + (records[y] as SingleMultipleSelectionVo).VALUE + "&nbsp;" + excluded + "</li>";
										}	
									}
									tip1+= "</ul>";
								}
								else
								{
									tip1 += empty;
								}
								
								//Range Header
								tip1 += "<br>" + rangeHeader;
								if((selection[x] as SelectionVo).value.rangeVo != null)
								{
									tip1+= "<ul>";
									if((selection[x] as SelectionVo).value.rangeVo.included == true)
									{
										tip1 += "<li>" +(selection[x] as SelectionVo).value.rangeVo.from.VALUE + " - " +
											(selection[x] as SelectionVo).value.rangeVo.to.VALUE + "&nbsp;" + included + "</li>";
									}
									else
									{
										tip1 += "<li>" + (selection[x] as SelectionVo).value.rangeVo.from.VALUE + " - " +
											(selection[x] as SelectionVo).value.rangeVo.to.VALUE + "&nbsp;" + excluded + "</li>";
									}
									tip1+= "</ul>";
								}
								else
								{
									tip1 += empty;
								}
								
								
								if((selection[x] as SelectionVo).value.singleMultipleSelection.length > 0 ||
									(selection[x] as SelectionVo).value.rangeVo != null)
									button.toolTip = tip1;
									
								else
									button.toolTip = null;
								
							}
						}
					}
				}
			}
			else
			{
				button.toolTip = null;
			}
		}
	}
}